package com.leon.learnenginespace.drools.utils;

import com.alibaba.fastjson.JSONArray;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 工具类
 * @author DJ032474
 */
public class MathUtils {
    /**
     * 获取BigDecimal类型值
     * @param value
     * @return
     */
    public static BigDecimal getBigDecimal(Object value ) {
        BigDecimal ret = null;  
        if( value != null ) {  
            if( value instanceof BigDecimal ) {  
                ret = (BigDecimal) value;  
            } else if( value instanceof String ) {  
                ret = new BigDecimal( (String) value );  
            } else if( value instanceof BigInteger) {
                ret = new BigDecimal( (BigInteger) value );  
            } else if( value instanceof Number ) {  
                ret = new BigDecimal( ((Number)value).doubleValue() );  
            } else {  
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");  
            }  
        }  
        return ret;  
    }

    /**
     * 获取长度
     * @param value
     * @return
     */
    public static int getLength(Object value ) {
        int valueSize = 0;
        if(ObjectUtils.isEmpty(value)){
            valueSize = 0;
        } else if(value instanceof Integer){
            valueSize = (Integer)value;
        } else if(value instanceof Long){
            valueSize =  Integer.valueOf(value.toString());
        } else if(value instanceof Short){
            valueSize = Integer.valueOf(value.toString());
        }  else if(value instanceof JSONArray){
            JSONArray array = (JSONArray)value;
            valueSize = array.size();
        } else if(value instanceof List){
            List array = (List)value;
            valueSize = array.size();
        } else {
            String valueStr = (String) value;
            valueSize = valueStr.length();
        }
        return valueSize;
    }



}  